<%--
  Created by IntelliJ IDEA.
  User: xf
  Date: 2016/5/6
  Time: 23:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Spring WebSocket</title>
    <script>
        var sock;
        $(function () {
            var url = 'ws://' + window.location.host + '/marco';
            if ('WebSocket' in window) {
                sock = new WebSocket(url);
            } else if ('MozWebSocket' in window) {
                sock = new MozWebSocket(url);
            } else {
                alert('你的浏览器不支持 WebSocket!');
                return;
            }
            console.log(url);

            sock.onopen = function () {
                $("#tips_content").html('WebSocket Create By XF ! <br />');
            };
            sock.onmessage = function (e) {
                $("#tips_content").append(e.data + '<br/>');
            };
            sock.onclose = function () {
                $("#tips_content").append('WebSocket Close !<br />');
            };
            function sayMarco() {
                sock.send("Marco!");
            }
        });

        function sendMsg() {
            var content = $("#msg").val();
            if (sock != null && content != '') {
                sock.send(content);
                $("#msg").val('');
            }
        }
        function clearScreen() {
            $("#tips_content").html('');
        }
    </script>
</head>
<body>
<div>
    <textarea class="form-control" id="msg" rows="10"></textarea>
    <button class="btn btn-primary" onclick="sendMsg()">Send</button>
    <button class="btn btn-default" onclick="clearScreen()">清屏</button>
</div>
<div class="alert alert-success" id="tips_content" role="alert"></div>
</body>
</html>
